package com.runshu.newsmartark.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.androidkun.xtablayout.XTabLayout;
import com.google.android.material.tabs.TabLayout;
import com.runshu.newsmartark.R;
import com.runshu.newsmartark.adapter.CpInvenFragmentPagerAdapter;
import com.runshu.newsmartark.base.BaseLazyFragmentPlus;
import com.runshu.newsmartark.entity.DeliveryTitieEntity;
import com.runshu.newsmartark.mvp.contract.DeliveryTitieContract;
import com.runshu.newsmartark.mvp.model.DeliveryTitieModel;
import com.runshu.newsmartark.mvp.presenter.DeliveryTitiePresenter;
import com.runshu.newsmartark.network.netbean.ResponseBean;
import com.runshu.newsmartark.util.common.ToastUtils;
import com.runshu.newsmartark.util.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Demo class
 *
 * @author yu
 * @date 2020/10/26
 * @decs：物流信息页面
 */

public class PhysicalManagerFragment extends BaseLazyFragmentPlus implements DeliveryTitieContract.View {


    @BindView(R.id.tab_my_phy)
    XTabLayout mTabMyPhy;
    @BindView(R.id.mViewPager)
    NoScrollViewPager mMViewPager;
    private ArrayList<Fragment> fragments;
    private List<String> tabList = new ArrayList<String>();
    private List<DeliveryTitieEntity> titleList;
    private DeliveryTitiePresenter titiePresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.frg_phy_manager_main;
    }

    @Override
    protected void initData() {
        titleList = new ArrayList<>();

    }

    @Override
    protected void initView(View layoutInflateView) {
        titiePresenter = new DeliveryTitiePresenter(this, new DeliveryTitieModel());
        titiePresenter.GetDeliveryTitie();
        //tab的字体选择器
        mTabMyPhy.setTabTextColors(getResources().getColor(R.color.color_A5A3E1), getResources().getColor(R.color.white));
        //下划线颜色
        mTabMyPhy.setSelectedTabIndicatorColor(getResources().getColor(R.color.white));

    }

    private void initFragment() {
        fragments = new ArrayList<Fragment>();
        fragments.clear();
        for (int i = 0; i < tabList.size(); i++) {
            PhysicalManagerItemFragment itemFragment = new PhysicalManagerItemFragment();
            Bundle bundle = new Bundle();
            bundle.putString("deliveryStatus", titleList.get(i).getValue());
            itemFragment.setArguments(bundle);
            fragments.add(itemFragment);
        }
        CpInvenFragmentPagerAdapter mAdapetr = new CpInvenFragmentPagerAdapter(getChildFragmentManager(), fragments, tabList);
        mMViewPager.setOffscreenPageLimit(tabList.size());
        mMViewPager.setAdapter(mAdapetr);
        mTabMyPhy.setupWithViewPager(mMViewPager);
        mMViewPager.setOnPageChangeListener(pageListener);
//        加载第一页的数据
        mMViewPager.setCurrentItem(0);
    }

    private void initTabColumn() {
        int count = tabList.size();
        if (count == 1) {
            mTabMyPhy.setTabMode(TabLayout.MODE_FIXED);
        } else {
            mTabMyPhy.setTabMode(TabLayout.MODE_SCROLLABLE);
        }
    }

    @Override
    protected void bindListener() {

    }

    /**
     * ViewPager监听
     */
    public ViewPager.OnPageChangeListener pageListener = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {


        }

        @Override
        public void onPageSelected(int position) {
            // TODO Auto-generated method stub
        }
    };

    @Override
    public void onGetDeliveryTitieSuccess(ResponseBean responseBean) {
        titleList = (List<DeliveryTitieEntity>) responseBean.pullData();
        for (int i = 0; i < titleList.size(); i++) {
            tabList.add(titleList.get(i).getLabel());
        }
        initTabColumn();
        initFragment();
    }

    @Override
    public void onError(String msg) {
        ToastUtils.showText(msg);
    }
}